package gui.ClassroomMenu;

import gui.ClassroomMenu.exceptions.LimitOvercome;
import gui.ClassroomMenu.exceptions.NoSelection;
import gui.ClassroomMenu.exceptions.NothingSelectedException;
import gui.ClassroomMenu.exceptions.NumOfResOvercomeException;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;

import model.basics.Resource;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class AddResIntoClassroom extends javax.swing.JPanel {
	private JList resList;
	private JList allResList;
	private JButton addButton;
	private JTextField removeCantTextField;
	private JLabel jLabel2;
	private JLabel crlLabel;
	private JTextField addCantTextField;
	private JButton removeButton;
	private Vector<Resource> allRes;
	private static HashMap<Resource, Integer> hmres;
	private static List<Resource>sysAllRes = model.search.System.getAllRes();
	private HashMap<Resource, Integer> sysGenRes = model.search.System.getResourceTotalGeneralList();

	public AddResIntoClassroom(HashMap<Resource, Integer> hm) {
		super();
		hmres = hm;
		allRes = new Vector<Resource>();
		initGUI();
	}
	
	public Vector<Resource> getAllRes() {
		return allRes;
	}

	public HashMap<Resource, Integer> getHmres() {
		return hmres;
	}

	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(373, 279));
			this.setLayout(null);
			{
				resList = new JList();
				this.add(resList);
//				List<Resource> sysAllRes = model.search.System.getAllRes();
				resList.setListData((Vector<String>)tosS(hmres,sysAllRes));
				resList.setBounds(12, 40, 132, 200);
			}
			{
				allResList = new JList();
				this.add(allResList);
				allResList.setBounds(218, 40, 140, 200);
//				List<Resource> sysAllRes = model.search.System.getAllRes();
				allResList.setListData((Vector<String>) tosS(
						model.search.System.getResourceTotalGeneralList(),
						sysAllRes));

			}
			{
				addButton = new JButton();
				this.add(addButton);
				addButton.setText("<--");
				addButton.setToolTipText("Add resources");
				addButton.setBounds(150, 83, 62, 21);
				addButton.addActionListener(new AddListener());
			}
			{
				removeButton = new JButton();
				this.add(removeButton);
				removeButton.setText("-->");
				removeButton.setToolTipText("Remove resources");
				removeButton.setBounds(150, 201, 62, 21);
				removeButton.addActionListener(new RemListener());
			}
			{
				addCantTextField = new JTextField();
				this.add(addCantTextField);
				addCantTextField.setBounds(150, 50, 62, 21);
				addCantTextField.setText("1");
			}
			{
				removeCantTextField = new JTextField();
				this.add(removeCantTextField);
				removeCantTextField.setBounds(150, 168, 62, 21);
				removeCantTextField.setText("1");
			}
			{
				crlLabel = new JLabel();
				this.add(crlLabel);
				crlLabel.setText("Classroom List");
				crlLabel.setBounds(12, 20, 115, 14);
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("General List");
				jLabel2.setBounds(218, 20, 115, 14);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private List<String> tosS(HashMap<Resource, Integer> res,
			List<Resource> allRes) {
		// transform the dictionary into a list of strings so in this way it can be shown
		List<String> ssr = new Vector<String>();

		for (int i = 0; i < allRes.size(); i++) {
			if (res.containsKey(allRes.get(i))) {
				String s = allRes.get(i).toString() + ": "
						+ res.get(allRes.get(i)).toString();
				ssr.add(s);
			}
		}
		if (ssr.isEmpty())
			ssr.add("There is no resources");
		return ssr;
	}

	public class AddListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				if (allResList.isSelectionEmpty())
					throw new NothingSelectedException();
				else{
					//ask for the limit of resources
					int index = ((String) allResList.getSelectedValue()).indexOf(":");
					String resS = ((String) allResList.getSelectedValue()).substring(index+2, (((String) allResList.getSelectedValue()).length()));
					Integer t= Integer.parseInt(resS);
					if(Integer.parseInt(addCantTextField.getText()) > t )
						throw new NumOfResOvercomeException();
					else {
//						List<Resource> sysAllRes = model.search.System.getAllRes();
//						HashMap<Resource, Integer> sysGenRes = model.search.System.getResourceTotalGeneralList();
					
						// generate the resource of the new classroom
						Resource res = createRes( (String) allResList.getSelectedValue());

						if (hmres.isEmpty()) 
							hmres.put(res, Integer.parseInt(addCantTextField
									.getText()));
						else{
							// if the resource does not exists
							if(! hmres.containsKey(res))
								hmres.put(res, Integer.parseInt(addCantTextField
										.getText()));
					
							// if the resource already exists
							else{		
								int nn = hmres.get(res);
								hmres.remove(res);
								hmres.put(res, nn
										+ Integer.parseInt(addCantTextField
												.getText()));
							}	
						}
						int n = sysGenRes.get(res);
						sysGenRes.remove(res);
						sysGenRes.put(res, n
								- Integer.parseInt(addCantTextField
										.getText()));
						// show in gui
						resList.setListData((Vector<String>) tosS(
								AddResIntoClassroom.hmres,sysAllRes));
						allResList.setListData((Vector<String>) tosS(sysGenRes,
								sysAllRes));
					}
				}
			} 
			catch(NumOfResOvercomeException e2){
				new LimitOvercome();
			}
			catch (NothingSelectedException e1) {
				new NoSelection();
			}
		}
	}

	public class RemListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				if (resList.isSelectionEmpty())
					throw new NothingSelectedException();
				else{
					int index = ((String) resList.getSelectedValue()).indexOf(":");
					String resS = ((String) resList.getSelectedValue()).substring(index+2, (((String) resList.getSelectedValue()).length()));
					Integer t= Integer.parseInt(resS);
					if(Integer.parseInt(removeCantTextField.getText()) > t )
						throw new NumOfResOvercomeException();
					else {
//						List<Resource> sysAllRes = model.search.System.getAllRes();
						HashMap<Resource, Integer> sysGenRes = model.search.System
								.getResourceTotalGeneralList();
	
						Resource res = createRes((String)resList.getSelectedValue());
						
						// if the resource already exists
						int nn = sysGenRes.get(res);
						sysGenRes.remove(res);
						sysGenRes.put(res, nn
								+ Integer.parseInt(removeCantTextField
										.getText()));
						
						// decrease number of rec in the general list
						int n = hmres.get(res);
						hmres.remove(res);
						if(n- Integer.parseInt(removeCantTextField.getText()) != 0) //remove in case there are no resource of this type
							hmres.put(res, n- Integer.parseInt(removeCantTextField.getText()));
						else
							allRes.remove(res);

						// show in gui
						resList.setListData((Vector<String>) tosS(
								AddResIntoClassroom.hmres, sysAllRes));
						allResList.setListData((Vector<String>) tosS(sysGenRes,
								sysAllRes));
					}
				}
			} 
			catch(NumOfResOvercomeException e2){
				new LimitOvercome();
			}
			catch (NothingSelectedException e1) {
				new NoSelection();
			}
		}
	}

	private static Resource createRes(String selectedValue) {
		// make things to use for the search of the indicate resource
		int index =  selectedValue.indexOf(":");
		String resS = selectedValue.substring(0, index);
		boolean b = true;
		int i =0;
		Resource res = null;
		while(b&& i< sysAllRes.size()){
			if(sysAllRes.get(i).getName().equals(resS)){
				res= sysAllRes.get(i);
				b=false;
			}
			i++;
		}
		return res;
	}
}
